package fr.upstlse.glre.client.widgets.fields;

import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;

import fr.upstlse.glre.client.RfgConstante;
import fr.upstlse.glre.client.vo.user.UserVOGWT;
import fr.upstlse.glre.client.widgets.modelData.UserModelData;

public class UserFields implements IUserFields {
	FormPanel formPanel;
	UserVOGWT user;
	
	TextField<String> id;
	TextField login;
	TextField motDePasse;
	ComboBox role;
		
	public UserFields(UserVOGWT user, FormPanel formPanel) {
		this.formPanel = formPanel;
		this.user = user;
		
		id = new TextField<String>();
		login = new TextField();
		motDePasse = new TextField();
		role = new ComboBox();
		
		createFields();
	}
	
	public void createFields() {
		id.setName(RfgConstante.ADMIN_UTILISATEURS_CREATION_USER_ID_NAME);
		id.setVisible(false);
		id.setLabelStyle("display: none");
		formPanel.add(id);
		
		login.setName(RfgConstante.ADMIN_UTILISATEURS_CREATION_USER_LOGIN_NAME);
		login.setFieldLabel(RfgConstante.ADMIN_UTILISATEURS_CREATION_USER_LOGIN_LABEL);
		formPanel.add(login);

		motDePasse.setPassword(true);
		motDePasse.setName(RfgConstante.ADMIN_UTILISATEURS_CREATION_USER_MOTDEPASSE_NAME);
		motDePasse
				.setFieldLabel(RfgConstante.ADMIN_UTILISATEURS_CREATION_USER_MOTDEPASSE_LABEL);
		formPanel.add(motDePasse);

		role.setName(RfgConstante.ADMIN_UTILISATEURS_CREATION_USER_ROLE_NAME);
		role.setFieldLabel(RfgConstante.ADMIN_UTILISATEURS_CREATION_USER_ROLE_LABEL);
		role.setDisplayField(RfgConstante.ROLE_MODELDATA_ROLE);
		ListStore<BaseModelData> lStore = new ListStore<BaseModelData>();
		lStore.add(UserModelData.getRoles());
		role.setStore(lStore);
		formPanel.add(role);
		
		if(user != null) {
			setFieldsValues();
		}
	}
	
	public void setFieldsValues() {
		id.setValue(new Long(user.getId()).toString());
		login.setValue(user.getLogin());
		motDePasse.setValue(user.getPassword());
		
		BaseModelData lRoleModelData = new BaseModelData();
		lRoleModelData.set(RfgConstante.ROLE_MODELDATA_ROLE, user.getRole());
		role.setValue(lRoleModelData);
	}
	
	

	public FormPanel getFormPanel() {
		return formPanel;
	}

	public void setFormPanel(FormPanel formPanel) {
		this.formPanel = formPanel;
	}

	public UserVOGWT getUser() {
		return user;
	}

	public void setUser(UserVOGWT user) {
		this.user = user;
	}

	public TextField<String> getId() {
		return id;
	}

	public void setId(TextField<String> pId) {
		id = pId;
	}

	public TextField getLogin() {
		return login;
	}

	public void setLogin(TextField login) {
		this.login = login;
	}

	public TextField getMotDePasse() {
		return motDePasse;
	}

	public void setMotDePasse(TextField motDePasse) {
		this.motDePasse = motDePasse;
	}

	public ComboBox getRole() {
		return role;
	}

	public void setRole(ComboBox role) {
		this.role = role;
	}

	public void removeFields() {
		// TODO Auto-generated method stub
		
	}
	
	public void addFields() {
		formPanel.add(id);
		formPanel.add(login);
		formPanel.add(motDePasse);
		formPanel.add(role);
	}
}
